package com.ruoyi.mall.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.mall.api.domian.Product;
import com.ruoyi.mall.domain.OrderCart;
import com.ruoyi.mall.service.IOrderCartService;
import com.ruoyi.mall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 购物车Controller
 *
 * @author luogoongquan
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/orderCart")
public class OrderCartController extends BaseController {
    @Autowired
    private IOrderCartService orderCartService;
    @Autowired
    private IProductService productService;

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("mall:orderCart:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderCart orderCart) {
        startPage();
        List<OrderCart> list = orderCartService.selectOrderCartList(orderCart);
        return getDataTable(list);
    }

    /**
     * 查询购物车列表
     */
    @GetMapping("wx/list")
    public AjaxResult wxList(OrderCart orderCart) {
        orderCart.setCustomerId(SecurityUtils.getUserId());
        List<OrderCart> list = orderCartService.selectOrderCartList(orderCart);
        return success(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("mall:orderCart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderCart orderCart) {
        List<OrderCart> list = orderCartService.selectOrderCartList(orderCart);
        ExcelUtil<OrderCart> util = new ExcelUtil<OrderCart>(OrderCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @RequiresPermissions("mall:orderCart:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderCartService.selectOrderCartById(id));
    }

    /**
     * 新增购物车
     */
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("wx")
    public AjaxResult add(@RequestBody OrderCart orderCart) {
        orderCart.setCustomerId(SecurityUtils.getUserId());
        return toAjax(orderCartService.insertOrderCart(orderCart));
    }

    /**
     * 修改购物车
     */
    @RequiresPermissions("mall:orderCart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderCart orderCart) {
        return toAjax(orderCartService.updateOrderCart(orderCart));
    }

    /**
     * 购物车商品数量修改
     *
     * @param orderCart
     * @return
     */
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PostMapping("wx/changeNum")
    public AjaxResult changeNum(@RequestBody OrderCart orderCart) {
        return success(orderCartService.changeNum(orderCart));
    }

    /**
     * 删除购物车商品
     */
//    @RequiresPermissions("mall:orderCart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @GetMapping("wx/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderCartService.deleteOrderCartByIds(ids));
    }

    /**
     * 获取订单确认界面商品信息
     *
     * @param ids
     * @param type 1：商品直接购买，2:购物车进入购买
     * @return
     */
    @GetMapping("wx/getConfirmInfo")
    public AjaxResult getOrderCartByIds(Long[] ids, Integer type) {
        if (type == 1) {
            Product product = productService.selectProductById(ids[0]);
            return success(Collections.singletonList(product));
        }
        return success(orderCartService.getOrderCartByIds(ids));
    }
}
